var expresionRegular = new Object();
var textos = new Array();
expresionRegular.texto = /[_a-zA-Z0-9 ]+/;
textos.texto = "its a required field";
expresionRegular.pass = /([_a-zA-Z]|[0-9]){2,}/;
textos.pass = "its a required field";
expresionRegular.numero = /^[0-9]+$/;
textos.numero = "its a required field that has to be a number";
expresionRegular.email = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
textos.email = "you must insert a valid email address";
expresionRegular.telefono = /^([\+]?[(]?[\+]?[ ]?[0-9]{2,3}[)]?[ ]?)?[0-9 ()\-]{4,25}$/;
textos.telefono = "you must insert a valid telephone number";
expresionRegular.tarjeta_credito = /^([0-9]{4}[- ]?){4}$/;
textos.tarjeta_credito = "you must insert a valid credit card number";
expresionRegular.codigo_postal = /^[0-9A-Za-z  -]{2,7}$/;
textos.codigo_postal = "you must insert a valid post code";
expresionRegular.importe = /^((([0-9]{1,3},)?([0-9]{3},)*([0-9]{1,3}){1,}(\.?[0-9]{0,6})?)|(\.?[0-9]{1,6})){1}$/;
textos.importe = "you must insert a valid amount";
expresionRegular.fecha = /^(([0-9]{1,2}(\-|\/|\.| )[0-9]{1,2}(\-|\/|\.| )[0-9]{2,4})|([0-9]{2,4}(\-|\/|\.| )[0-9]{1,2}(\-|\/|\.| )[0-9]{1,2})){1}$/;
textos.fecha = "you must insert the date in the specied format";
expresionRegular.ip = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
textos.ip = "you must insert a valid ip address";


function contarCamposSeleccionMultiple(elFormulario,campo)
{
	var elementosFormulario2 = elFormulario.elements; 
	var otro_i = 0;
	for(var i = 0; i < elementosFormulario2.length; i++){
		with(elementosFormulario2[i]){
			var nombre = elementosFormulario2[i].name;
			if(campo == nombre){otro_i++;}
		}
	}	
	return otro_i;
}



var pasadaValidacion = 1;
var estiloOriginales = new Array();
var camposEnFormulario = new Array();


function validarFormulario(elFormulario) 
{
	var elementosFormulario = elFormulario.elements;
	var contar = 0;
	var primer_elemento = 0;
	var encontrados = 0;
	var anterior = '';
	var tiposCampos = new Array();
	var seleccionables = new Array();
	var resultados = new Array();
	var nombres = new Array();	
	
	var mensaje = '';
		mensaje  = "_______________________________________________________________\n\n"
		mensaje += "The form was not submitted because of the following error(s).\n";
		mensaje += "Please correct these error(s) and re-submit.\n";
		mensaje += "_______________________________________________________________\n\n"
	for(var i = 0; i < elementosFormulario.length; i++) {
		with(elementosFormulario[i]) { 
			var temporal1 = elementosFormulario[i].alt;
			
			if(!temporal1) continue;
			
			var temporal2 = temporal1.split(/partirporaqui/g)
			
			var v = temporal2[0];
			var tipoCampo = elementosFormulario[i].type;
			var claseCampo = elementosFormulario[i].className;
			var nombreCampo = elementosFormulario[i].name;
			
			if(!camposEnFormulario[nombreCampo]){
				estiloOriginales[nombreCampo] = claseCampo;
			}
			
			camposEnFormulario[nombreCampo] = pasadaValidacion;

			var descrip = temporal2[1];
			if(v=="" | descrip=="" | !v | !descrip) continue;
			
			if(tipoCampo == "checkbox"|| tipoCampo == "radio"){				
				if(anterior != name){
					seleccionables = 0;
					arraySeleccionables = new Array();
					nContador = 0;			
					anterior = name;
            	}            		
            	seleccionables++;
				if(!checked){seleccionables--;}	
				nContador++;
				numerillo = contarCamposSeleccionMultiple(elFormulario,name);
				if((nContador == numerillo) && (seleccionables==0)){
            	  	if(anterior != descrip){				
						tiposCampos[i] = tipoCampo;
            	  		encontrados++;
 						mensaje +=  encontrados+":: "+descrip +":  "+ textos[v]+"\n";
 						
 						if (document.all && document.all[''+nombreCampo+'formError']) {
 							document.all[''+nombreCampo+'formError'].innerHTML='';
 							document.all[''+nombreCampo+'formError'].outerHTML='';
 						}
 						if (document.all){
 							elementosFormulario[i].parentElement.insertAdjacentHTML("afterBegin", "<div class=formError name="+nombreCampo+"formError id="+nombreCampo+"formError>"+textos[v]+"</div>");
 							elementosFormulario[i].parentElement.className = estiloOriginales[nombreCampo]+"Warn";
 						}
 						
 						anterior = descrip;
 						if(!contar) {
							contar = i;
							if(contar == 0){
								primer_elemento = 1;
							}							
 						}
 					}
				}else{
 					if (document.all && document.all[''+nombreCampo+'formError']) {
 						document.all[''+nombreCampo+'formError'].innerHTML='';
 						document.all[''+nombreCampo+'formError'].outerHTML='';
 					}
 					if (document.all){
 						elementosFormulario[i].parentElement.className = estiloOriginales[nombreCampo];
 					}
				}
			}
			else{
				var laExpresion = expresionRegular[v]; 
				var loTenemos = laExpresion.exec(value); 			
				var e = 1+i;
				var f = 1-i;
				if(!loTenemos){
            	  	if(anterior != descrip){
						tiposCampos[i] = tipoCampo;
            	  		encontrados++;
 						mensaje +=  encontrados+":: "+descrip +":  "+ textos[v]+"\n";

 						if (document.all && document.all[''+nombreCampo+'formError']) {
 							document.all[''+nombreCampo+'formError'].innerHTML='';
 							document.all[''+nombreCampo+'formError'].outerHTML='';
 							}
 						if (document.all){
 							elementosFormulario[i].insertAdjacentHTML("beforeBegin", "<div class=formError name="+nombreCampo+"formError id="+nombreCampo+"formError>"+textos[v]+"</div>");
 						}
 						
 						elementosFormulario[i].className = estiloOriginales[nombreCampo]+"Warn";
 						anterior = descrip;
 						if(!contar) {
							contar = i;
							if(contar == 0){
								primer_elemento = 1;
							}
 						}
 					}
				}else{
 					if (document.all && document.all[''+nombreCampo+'formError']) {
 						document.all[''+nombreCampo+'formError'].innerHTML=''
 						document.all[''+nombreCampo+'formError'].outerHTML=''
 					}
					elementosFormulario[i].className = estiloOriginales[nombreCampo];
 				}
 			}
		}
	}
	
	pasadaValidacion++;
	
	if(contar | primer_elemento){   
		alert(mensaje);
		if(tiposCampos[contar] == "text" || tiposCampos[contar] == "textarea"){
			if(primer_elemento){
				contar = 0;
			}
			//elementosFormulario[contar].select();
			elementosFormulario[contar].focus();
		}
		return false; 		
	}
	else{
		return true;
	}
}

